package chenji.postoffice.mailcore;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashSet;

public class Mail implements Comparable<Mail> {
	private String Message;
	private LinkedHashSet<String> MessageSet = new LinkedHashSet<String>();
	private String FromName;
	private String ToName;
	private long Date;
	private String Subject;
	private boolean readDone = false;
	private String ID;

	boolean b;

	public boolean getb() {
		return this.b;
	}

	public String getDate() {
		if (this.Date == 0) {
			return null;
		}
		SimpleDateFormat ab = new SimpleDateFormat("yy-MM-dd hh:mm:ss ");

		return ab.format(this.Date);
	}

	public Mail(String FormName) {
		this.FromName = FormName;

	}

	public void SetToName(String toName) {
		this.ToName = toName;
	}

	public void WriteDoneand() {
		this.Date = new Date().getTime();
		SimpleDateFormat ab = new SimpleDateFormat("yyMM-ddhh-mmss");
		this.ID = ab.format(this.Date);
		this.ID = ID + "-" + this.FromName + "-" + this.ToName;

	}

	public void setSubject(String Subject) {
		this.Subject = Subject;
	}

	public Mail(String Message, String FromName, String ToName, String Subject, int readDone, long Date, String ID) {
		this.Message = Message;
		String[] Me = Message.split("#");
		for (String s : Me) {
			MessageSet.add(s);
		}
		this.FromName = FromName.toLowerCase();
		this.ToName = ToName.toLowerCase();
		this.Subject = Subject;
		if (readDone == 0) {
			this.readDone = false;

		} else {
			this.readDone = true;
		}
		this.Date = Date;
		this.ID = ID;
	}

	public String getMessage() {
		this.Message = null;
		for (String s : MessageSet) {
			if (Message == null) {
				Message = s + "#";
			} else {
				Message = Message + s + "#";
			}
		}
		Message = Message.substring(0, Message.length() - 1);
		return Message;
	}

	public String getTo() {
		return this.ToName.toLowerCase();
	}

	public boolean readdone() {
		return readDone;
	}

	public String getFormName() {
		return this.FromName.toLowerCase();
	}

	public String getSubject() {
		return this.Subject;
	}

	public void addLine(String s) {
		MessageSet.add(s);
	}

	public LinkedHashSet<String> getMessageSet() {
		return MessageSet;

	}

	public int compareTo(Mail arg0) {
		if (this.readdone() && arg0.readdone()) {
			if (arg0.Date < this.Date) {
				return -1;
			}
			if (arg0.Date == this.Date) {
				return 0;
			} else {
				return 1;
			}
		}
		if (this.readdone() == false && arg0.readdone() == false) {
			if (arg0.Date < this.Date) {
				return -1;
			}
			if (arg0.Date == this.Date) {
				return 0;
			} else {
				return 1;
			}
		}
		if (this.readdone() == false && arg0.readdone() == true) {
			return -1;
		}
		if (this.readdone() == true && arg0.readdone() == false) {
			return 1;
		}
		return 0;
	}

	public long getDateInLong() {

		return this.Date;
	}

	public String getID() {
		return ID;
	}

	public int getLine() {
		return this.MessageSet.size();
	}

}
